/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.utils;

import ca.trader.regions.Country;
import ca.trader.regions.Location;
import ca.trader.regions.Region;
import ca.trader.sale.Sale;
import ca.trader.sale.SaleCategoryEnum;
import ca.trader.startup.ServletContextManager;
import ca.trader.except.LocationNotFoundException;
import ca.trader.user.User;
import java.math.BigDecimal;

/**
 *
 * @author Ahsan
 */
public class ObjectFactory
{

	 public static User createUser(String email, String firstName, String lastName, String cryptedPassword, boolean activated, String country, String region, String city) throws LocationNotFoundException
	 {
		  User user = new User();
		  user.setEmail(email);
		  user.setFirstName(firstName);
		  user.setLastName(lastName);
		  user.setLocation(createLocationFromNames(country, region, city));
		  user.setCryptedPassword(cryptedPassword);
		  

		  user.setActivated(true);
		  

		  return user;
	 }

	 public static Location createLocationFromNames(String countryName, String regionName, String cityName) throws LocationNotFoundException
	 {
		  Country country = ServletContextManager.getRegionManager().getCountry(countryName);
		  
		  if (country == null)
		  {
			   throw new LocationNotFoundException ("Country " + countryName + " does not exist.");
		  }
			   
		  Region region = country.getRegion(regionName);
		  		  
		  if (region == null)
		  {
			   throw new LocationNotFoundException ("Region " + regionName + " does not exist.");
		  }
				  
		  if (!region.containsCity(cityName))
		  {
			   	throw new LocationNotFoundException ("City " + cityName + " does not exist.");

		  }
		  
		  return new Location (country, region, cityName);
	 }
	 
	 /**
	  *
	  * @param seller
	  * @param saleCategory
	  * @param saleTitle
	  * @param saleDesc
	  * @param salePrice
	  * @param saleLocation
	  * @return
	  */
	 public static Sale createSale (User seller, SaleCategoryEnum saleCategory, String saleTitle, String saleDesc, BigDecimal salePrice, Location saleLocation)
	 {
		  Sale sale = new Sale ();
		  
		  sale.setSeller(seller);
		  sale.setDescription(saleDesc);
		  sale.setTitle(saleTitle);
		  sale.setCategory(saleCategory);
		  sale.setPrice(salePrice);
		  sale.setLocation(saleLocation);
		  
		  return sale;
	 }

}
